/** 京东app任务 
 *TODO  接口参数有签名处理， 未破
 */
// let dateUtil = require('./dateUtil.js');
let {getForString, getForJson, postForJson} = require('./jdClient.js');

/** 进店领豆 */
function queryTaskIndex() {
    let url = "https://api.m.jd.com/client.action?functionId=queryTaskIndex&clientVersion=9.0.4&build=73764&client=android&d_brand=Xiaomi&d_model=MI6&osVersion=9&screen=1920*1080&partner=tencent&aid=a1893ed6f4dbc682&oaid=9646de4473ddf7cc&eid=eidAd74d812162s3+6nygm9TRJaswEPkVpLhQfup8/P1yxlN19lmwg+AEeW3OWl4XDjLPHdRNhUKVFG/3Y4B2OyHQzM8ggdUcBQFDA5zYZJcs3hP+MZX&sdkVersion=28&lang=zh_CN&uuid=865441031402480-4c49e351c106&area=19_1601_3633_63252&networkType=wifi&wifiBssid=6c0c202155450f3a51fb8d2e5d93c43b&uts=0f31TVRjBSsqndu4%2FjgUPz6uymy50MQJ8pcDt0HWFSDEz05CQaZpQ294NhtIj34uhZl0zJfyN3tEXsMfICFLMG6tpCbo89VQ0WtjP8QWJKfArTNLH3U1P6HgbffCyyxrVuzHpMv72LA0kqe7B6HDQX21mf3E596eyRSaQZjqxlyg9WoA%2FXQ6LzrLEQLgPPfaUTTOzm07Y6%2F5YSG1JG%2BkdQ%3D%3D&st=1593876399864&sign=42286531abdb41bf5a191d23b179d310&sv=121";
    let data = {};
    data = "body=" + encodeURIComponent(JSON.stringify(data));
    let response = postForJson(url, data);
    if (response.code == 0 && response.data) {
        // log(response)
        let taskList = response.data.taskList;
        taskList.forEach((task, index) => {
            if (task.taskStatus == 1) {
                // 进店领豆
                let shopId = task.shopId;
                takeTask(shopId, task.taskId);
                getShopHomeActivityInfo(shopId);
                followShop(shopId);
            } else if (task.taskStatus == 3) {
                console.log('进店【' + task.shopName + '】已完成');
            } else {
                console.log('未知的进店状态：' + JSON.stringify(task));
            }
        });
        // log(taskList)
        // console.log('收取任务奖励：' + award.name + award.count + '个');
    } else {
        console.log('收取任务奖励失败: ' + JSON.stringify(response));
    }
}
/** 进店领豆 */
function takeTask(shopId, taskId) {
    let url = "https://api.m.jd.com/client.action?functionId=takeTask&clientVersion=9.0.4&build=73764&client=android&d_brand=Xiaomi&d_model=MI6&osVersion=9&screen=1920*1080&partner=tencent&aid=a1893ed6f4dbc682&oaid=9646de4473ddf7cc&eid=eidAd74d812162s3+6nygm9TRJaswEPkVpLhQfup8/P1yxlN19lmwg+AEeW3OWl4XDjLPHdRNhUKVFG/3Y4B2OyHQzM8ggdUcBQFDA5zYZJcs3hP+MZX&sdkVersion=28&lang=zh_CN&uuid=865441031402480-4c49e351c106&area=19_1601_3633_63252&networkType=wifi&wifiBssid=6c0c202155450f3a51fb8d2e5d93c43b&uts=0f31TVRjBSsqndu4%2FjgUPz6uymy50MQJ8pcDt0HWFSDEz05CQaZpQ294NhtIj34uhZl0zJfyN3tEXsMfICFLMG6tpCbo89VQ0WtjP8QWJKfArTNLH3U1P6HgbffCyyxrVuzHpMv72LA0kqe7B6HDQX21mf3E596eyRSaQZjqxlyg9WoA%2FXQ6LzrLEQLgPPfaUTTOzm07Y6%2F5YSG1JG%2BkdQ%3D%3D&st=1593878576259&sign=c09f331bdb4f35c3b9f19b4d6d50c3ca&sv=111";
    let data = {"taskId": taskId + ""};
    data = "body=" + encodeURIComponent(JSON.stringify(data));
    let response = postForJson(url, data);
    log(response)
    if (response.code == 0) {
        console.log('进店领豆成功');
    } else {
        console.log('进店领豆失败: ' + JSON.stringify(response));
    }
}

/** 关注店铺 */
function getShopHomeActivityInfo(shopId) {
    let url = "https://api.m.jd.com/client.action?functionId=getShopHomeActivityInfo&clientVersion=9.0.4&build=73764&client=android&d_brand=Xiaomi&d_model=MI6&osVersion=9&screen=1920*1080&partner=tencent&aid=a1893ed6f4dbc682&oaid=9646de4473ddf7cc&eid=eidAd74d812162s3+6nygm9TRJaswEPkVpLhQfup8/P1yxlN19lmwg+AEeW3OWl4XDjLPHdRNhUKVFG/3Y4B2OyHQzM8ggdUcBQFDA5zYZJcs3hP+MZX&sdkVersion=28&lang=zh_CN&uuid=865441031402480-4c49e351c106&area=19_1601_3633_63252&networkType=wifi&wifiBssid=6c0c202155450f3a51fb8d2e5d93c43b&uts=0f31TVRjBSsqndu4%2FjgUPz6uymy50MQJ8pcDt0HWFSDEz05CQaZpQ294NhtIj34uhZl0zJfyN3tEXsMfICFLMG6tpCbo89VQ0WtjP8QWJKfArTNLH3U1P6HgbffCyyxrVuzHpMv72LA0kqe7B6HDQX21mf3E596eyRSaQZjqxlyg9WoA%2FXQ6LzrLEQLgPPfaUTTOzm07Y6%2F5YSG1JG%2BkdQ%3D%3D&st=1593877143953&sign=ffe627c204052dbb293446a1b070b2ea&sv=121";
    let data = {
        "RNVersion": "0.59.9",
        "abtest": "",
        "lat": "23.130501",
        "latWs": "23.129837",
        "lng": "113.391874",
        "lngWs": "113.392546",
        "refer": "JingDou_JingDou",
        "shopId": shopId + "",
        "source": "app-shop",
        "sourceRpc": "shop_app_home_home"
    };
    data = "body=" + encodeURIComponent(JSON.stringify(data));
    let response = postForJson(url, data);
    if (response.isSuccess && response.result) {
        if (response.result.followed) {
            console.log("已关注店铺");
            drawShopGift(shopId);
        } else {
            console.log("未关注店铺");
            drawShopGift(shopId);
        }
    } else {
        console.log('getShopHomeActivityInfo失败: ' + JSON.stringify(response));
    }
}

/** 取消关注店铺 */
function drawShopGift(shopId) {
    let url = "https://api.m.jd.com/client.action?functionId=drawShopGift&clientVersion=9.0.4&build=73764&client=android&d_brand=Xiaomi&d_model=MI6&osVersion=9&screen=1920*1080&partner=tencent&aid=a1893ed6f4dbc682&oaid=9646de4473ddf7cc&eid=eidAd74d812162s3+6nygm9TRJaswEPkVpLhQfup8/P1yxlN19lmwg+AEeW3OWl4XDjLPHdRNhUKVFG/3Y4B2OyHQzM8ggdUcBQFDA5zYZJcs3hP+MZX&sdkVersion=28&lang=zh_CN&uuid=865441031402480-4c49e351c106&area=19_1601_3633_63252&networkType=wifi&wifiBssid=6c0c202155450f3a51fb8d2e5d93c43b&uts=0f31TVRjBSsqndu4%2FjgUPz6uymy50MQJ8pcDt0HWFSDEz05CQaZpQ294NhtIj34uhZl0zJfyN3tEXsMfICFLMG6tpCbo89VQ0WtjP8QWJKfArTNLH3U1P6HgbffCyyxrVuzHpMv72LA0kqe7B6HDQX21mf3E596eyRSaQZjqxlyg9WoA%2FXQ6LzrLEQLgPPfaUTTOzm07Y6%2F5YSG1JG%2BkdQ%3D%3D&st=1593877212051&sign=acb7a905c57be1e84f07cf0dc3f139b4&sv=111";
    let data = {
        "activityId": 801748,
        "follow": false,
        "shopId": shopId + "",
        "sourceRpc": "shop_app_home_window",
        "venderId": "1000078171"
    };
    data = "body=" + encodeURIComponent(JSON.stringify(data));
    let response = postForJson(url, data);
    if (response.isSuccess && response.result) {
        // log(response)
        if (response.result.followResult) {
            if (response.result.giftResult) {
                console.log('关注店铺成功:获得' + response.result.redWord + '个' + response.result.rearWord);
            } else {
                console.log('关注店铺成功:' + response.result.giftDesc);
            }
        } else {
            console.log(response.result.followDesc);
        }
    } else {
        console.log('drawShopGift失败: ' + JSON.stringify(response));
    }
}

function followShop(shopId) {
    let url = "https://api.m.jd.com/client.action?functionId=followShop&clientVersion=9.0.4&build=73764&client=android&d_brand=Xiaomi&d_model=MI6&osVersion=9&screen=1920*1080&partner=tencent&aid=a1893ed6f4dbc682&oaid=9646de4473ddf7cc&eid=eidAd74d812162s3+6nygm9TRJaswEPkVpLhQfup8/P1yxlN19lmwg+AEeW3OWl4XDjLPHdRNhUKVFG/3Y4B2OyHQzM8ggdUcBQFDA5zYZJcs3hP+MZX&sdkVersion=28&lang=zh_CN&uuid=865441031402480-4c49e351c106&area=19_1601_3633_63252&networkType=wifi&wifiBssid=6c0c202155450f3a51fb8d2e5d93c43b&uts=0f31TVRjBSsqndu4%2FjgUPz6uymy50MQJ8pcDt0HWFSDEz05CQaZpQ294NhtIj34uhZl0zJfyN3tEXsMfICFLMG6tpCbo89VQ0WtjP8QWJKfArTNLH3U1P6HgbffCyyxrVuzHpMv72LA0kqe7B6HDQX21mf3E596eyRSaQZjqxlyg9WoA%2FXQ6LzrLEQLgPPfaUTTOzm07Y6%2F5YSG1JG%2BkdQ%3D%3D&st=1593877999484&sign=8df9e6a873cadb5d78203f0f136560f3&sv=102";
    let data = {
        "follow": false,
        "shopId": shopId + "",
        "sourceRpc": "shop_app_home_follow"
    };
    data = "body=" + encodeURIComponent(JSON.stringify(data));
    let response = postForJson(url, data);
    if (response.code == 0) {
        if (response.optCode == 'F10000') {
            console.log('取消关注店铺成功');
        } else {
            console.log(response.msg);
        }
    } else {
        console.log('followShop失败: ' + JSON.stringify(response));
    }
}


queryTaskIndex();
// let shopid = "1000078171";
// getShopHomeActivityInfo(shopid);
// followShop(shopid);